home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Client.java < prev    next >
Text File  |  1998-10-28  |  6KB  |  205 lines

  1. /*
  2.  * Title: Conversing Applets
  3.  * Type: Applet
  4.  * Source: Client.java
  5.  * Application Description:
  6.  * The Conversing Applets project provides a simple and effective way
  7.  * to allow applets to communicate within the same "environment", i.e.
  8.  * within the same browser page.
  9.  *
  10.  * This is done using a Mediator class as the background manager and
  11.  * other visible classes which display results. The Mediator class uses
  12.  * static variables to keep track of all the events taking place in
  13.  * these visible applets.
  14.  *
  15.  * In this example, two visible applets are used along with the one
  16.  * Mediator class. The Developer and Client class generate text strings which
  17.  * represent conversation.  The Mediator object(s) take those text
  18.  * strings and distribute them to the other applet (Developer to Client and
  19.  * Client to Developer).
  20.  *
  21.  * The result is two distinct applet objects transmitting data back and
  22.  * forth. This sort of project can be very useful in developing more
  23.  * interesting Web pages as well as Web page applets which gather
  24.  * information about usage, etc.  The Mediator class concept can be
  25.  * built upon to meet the needs of more specific Java-based Web
  26.  * applications.
  27.  *
  28.  * Symantec Corporation.
  29.  */
  30.  
  31.  
  32. import java.applet.Applet;
  33. import java.awt.*;
  34.  
  35. /**
  36.  * The Client applet class
  37.  */
  38.  
  39. public class Client extends Applet implements Runnable
  40. {
  41.         Mediator myMediator;            //Local copy of the Mediator object
  42.         String label, response;         //Various applet text variables
  43.         int theNumber;                  //Used to track a random number
  44.         Thread clientThread;             //The main applet thread
  45.  
  46.  
  47.         /**
  48.          * Set up the applet layout and initialize variables
  49.          */
  50.  
  51.         public void init()
  52.         {
  53.            myMediator = new Mediator();
  54.  
  55.            //{{INIT_CONTROLS
  56.         setLayout(null);
  57.         setSize(448,110);
  58.         clientTalk.setEditable(false);
  59.         add(clientTalk);
  60.         clientTalk.setBounds(170,38,268,24);
  61.         client.setText("Client says:");
  62.         add(client);
  63.         client.setBounds(0,35,80,24);
  64.         top.setText("I\'m a Client");
  65.         add(top);
  66.         top.setBounds(0,0,89,24);
  67.         developer.setText("The Developer just said:");
  68.         add(developer);
  69.         developer.setBounds(0,70,153,24);
  70.         developerTalk.setEditable(false);
  71.         add(developerTalk);
  72.         developerTalk.setBounds(170,68,268,24);
  73.         //}}
  74.  
  75.         }
  76.  
  77.  
  78.         /**
  79.          * Start the main thread
  80.          */
  81.  
  82.         public void start()
  83.         {
  84.            clientThread = new Thread(this);
  85.            clientThread.start();
  86.         }
  87.  
  88.  
  89.  
  90.  
  91.          /**
  92.           * The applet action takes place here. The applet waits, gets a
  93.           * random number from the Mediator, uses that to id a piece of
  94.           * conversation and then updates the UI elements.
  95.           */
  96.  
  97.          public void run()
  98.          {
  99.             Thread clientThread = Thread.currentThread();
  100.             while(true)
  101.             {
  102.                 try
  103.                 {
  104.                     clientThread.sleep(2000);
  105.                 } catch(Exception e)
  106.                 {
  107.                     System.out.println(e);
  108.                 }
  109.  
  110.                 //Keep a copy of the last random number
  111.                 myMediator.oldNumber = myMediator.theNumber;
  112.                 myMediator.theNumber = myMediator.genRandom();
  113.  
  114.                 checkWithMediator();
  115.  
  116.                 takeAction();
  117.  
  118.             }
  119.          }
  120.  
  121.  
  122.          /**
  123.           * A helper function which uses the new Mediator random number
  124.           * to set the conversation text
  125.           */
  126.  
  127.          public synchronized void checkWithMediator()
  128.          {
  129.  
  130.             int index = myMediator.theNumber;
  131.  
  132.  
  133.             if(index < 10)
  134.             {
  135.                 label = "Isn't Visual CafΘ great?";
  136.             } else if((index <= 20) && (index >= 10))
  137.             {
  138.                 label = "The Visual CafΘ compiler is sooo FAST!";
  139.             } else if((index <= 30) && (index > 20))
  140.             {
  141.                 label = "Check out the snazzy class editor.";
  142.             } else if((index <= 40) && (index > 30))
  143.             {
  144.                 label = "Debugging in Visual CafΘ is easy.";
  145.             } else if((index <= 50) && (index > 40))
  146.             {
  147.                 label = "Visual CafΘ is Great!";
  148.             } else if((index <= 60) && (index > 50))
  149.             {
  150.                 label = "What do you mean by that?";
  151.             } else if((index <= 70) && (index > 60))
  152.             {
  153.                 label = "I want to make sure you use Visual CafΘ.";
  154.             } else if((index <= 80) && (index > 70))
  155.             {
  156.                 label = "Isn't that the truth";
  157.             } else if((index <= 90) && (index > 80))
  158.             {
  159.                 label = "Wow, really!!!";
  160.             } else if((index <= 100) && (index >90))
  161.             {
  162.                 label = "That's very cool";
  163.             }
  164.  
  165.             //Let the Mediator know what the client said
  166.             myMediator.setClientTalk(label);
  167.  
  168.             //Find out what the developer said
  169.             response = myMediator.getDeveloperTalk();
  170.          }
  171.  
  172.  
  173.  
  174.          /**
  175.           * A helper method which updates the UI elements
  176.           */
  177.  
  178.          public void takeAction()
  179.          {
  180.             clientTalk.setText(label);
  181.             developerTalk.setText(response);
  182.          }
  183.  
  184.         //{{DECLARE_CONTROLS
  185.     java.awt.TextField clientTalk = new java.awt.TextField(18);
  186.     java.awt.Label client = new java.awt.Label();
  187.     java.awt.Label top = new java.awt.Label();
  188.     java.awt.Label developer = new java.awt.Label();
  189.     java.awt.TextField developerTalk = new java.awt.TextField(17);
  190.     //}}
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.